package com.ceadeal.admin.common.api;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 功能描述: Api接口返回结果封装
 *
 * 作者: Yzw
 * 日期: 2017-03-01 16:19:07
 */
public class ApiResult {

    private int code = -1;
    private String message;
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Object data;

    public static ApiResult success() {
        return new ApiResult(ApiResultEnum.SUCCESS, null);
    }

    public static ApiResult success(Object data) {
        return new ApiResult(ApiResultEnum.SUCCESS, data);
    }

    public static ApiResult failure(ApiResultEnum code) {
        return new ApiResult(code, null);
    }

    public static ApiResult failure(ApiResultEnum code, Object data) {
        return new ApiResult(code, data);
    }

    public static ApiResult paramError() {
        return new ApiResult(ApiResultEnum.PARAMETER_ERROR, null);
    }

    public static ApiResult paramError(Object data) {
        return new ApiResult(ApiResultEnum.PARAMETER_ERROR, data);
    }

    public static ApiResult internalError() {
        return new ApiResult(ApiResultEnum.INTERNAL_ERROR, null);
    }

    public static ApiResult internalError(Object data) {
        return new ApiResult(ApiResultEnum.INTERNAL_ERROR, data);
    }

    public static ApiResult loginError() {
        return new ApiResult(ApiResultEnum.LOGIN_ERROR, null);
    }

    public ApiResult(ApiResultEnum code, Object data) {
        this.code = code.getCode();
        this.message = code.getMsg();
        this.data = data;
    }

    public ApiResult(int code, String msg, Object data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setCode(ApiResultEnum code) {
        this.code = code.getCode();
        this.message = code.getMsg();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ApiResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
